(function (G) {
    const javaUrl = G.Storage.get('JAVAURL')
    G.vue({
        data: {
            userInfo: {},
            user: {},
            groups: [],
            loaded: false,
            count:0,
            types:[],
            taskCounts:{},
            code:0,
            all:'',

            APIURL: G.Storage.get('APIURL'),
            keyWord: '',
            tabList: [],
            cur: 0,
            listArr: [],
            pageCount: 1,
            page: 1,
            tabCur: 0,

            safeKownTabs: [],
            safeKownList: [],
            safeKnowCur: 0,
            safeKnowPage: 1,
            isKnowMore: false,

            triggered: false,
            dealType:'pull',

            tzggList:[]
        },
        methods: {
            go(event){
                let url = event.currentTarget.dataset.url
                if(url == '/pages/instruction/edit/index'){
                    G.confirm('请选择下达方式？',{
                        cancel(){
                            G.$go('/pages/gycase/addAndKoufen/index?type=2')
                        },
                        cancelText:'直接扣分',
                        confirmText:'指令下达'
                    }).then(res => {
                        G.$go(url)
                    })
                }else{
                    G.$go(url)
                }
            },
            // 获取通知公告
            getAqgg(){
                G.get('/api/model/article', {
                    type:{$in:['notify','notice']},
                    hot:1,
                    status:1
                }).then(list => {
                    this.setData({
                    tzggList:list
                    })
                })
            },
            gototzggList(){
                const data = JSON.stringify(this.data.tzggList)
                wx.navigateTo({
                    url: '/pages/tzggList/tzggList?data=' + data
                })
            },
            gotoaq(){
              wx.navigateTo({
                url: '/pages/safetyTraining/index/index'
              })
            },
            gotodealt(){
              wx.switchTab({
                url: '/pages/dealt/dealt',
              })  
            },
            onPullDownRefresh(){
                this.load()
                this.setData({
                  safeKownList:[]
                })
                this.getSafeKnow()
                this.getAqgg()
                wx.stopPullDownRefresh()
            },
            // 安全常识详情页面
            toDetail(e) {
                const {
                    tabCur,
                    APIURL
                } = this.data
                const {
                    item
                } = e.currentTarget.dataset
                let that = this
                console.log(item)
                if (tabCur == 0) {
                    wx.navigateTo({
                        url: '/pages/safetyTraining/safeKnowDetail/index?id=' + item.id 
                    })
                    G.Storage.set('isToDetail',true)
                } else {
                    if (item.fileType == 'video') {
                        wx.navigateTo({
                            url: '/pages/safetyTraining/videoDetail/index?id=' + item.id + '&fileid=' + item.fileId + '&title=' + item.title
                        })
                        return
                    }
                    wx.downloadFile({
                        url: APIURL + '/file/' + item.fileId,
                        success: function (res) {
                            const filePath = res.tempFilePath
                            wx.openDocument({
                                filePath: filePath,
                                fileType: item.fileType,
                                success: function (res) {
                                    console.log('打开文档成功')
                                    that.stduyStatic(item)
                                }
                            })
                        }
                    })
                }

            },
            // 获取安全常识
            // getSafeKnow() {
            //     const {
            //         keyWord,
            //         comm_type,
            //         safeKnowPage,
            //         safeKownList
            //     } = this.data
            //     let params = {
            //         pageNum: safeKnowPage,
            //         pageSize: 10,
            //         condition: keyWord,
            //         comm_type
            //     }
            //     G.get(javaUrl + '/security/get_scs', params).then(rej => {
            //         let newList = rej.rows && rej.rows.map(item => {
            //             return {
            //                 iconFileId: item.icon_id,
            //                 title: item.comm_title,
            //                 time: item.createTimeString,
            //                 noText: `观看 ${item.visit_nos || 0} 次`,
            //                 id: item.id,
            //                 content: item.comm_content,
            //                 visit_nos:item.visit_nos || 0
            //             }
            //         })
            //         let list = [...safeKownList, ...newList]
            //         let isKnowMore = true
            //         if (rej.rows && rej.rows.length < 10) isKnowMore = false
            //         this.setData({
            //             listArr: list,
            //             safeKownList: list,
            //             isKnowMore,
            //             triggered:false
            //         })


            //     })
            // },
            // 功能入口--“全部”按钮逻辑
            ShowAll(){
                this.setData({
                    code:100,
                    all:'none'
                })
            },
            /**
             * 页面上拉触底事件的处理函数
             */
            onReachBottom () {
                console.log('上拉刷新')
            },
            scrolltolower() {
                console.log('滚动到底部')
            },
            getGroups() {
                if(this.data.groups.length) return false
                G.get('/api/model/group_user', {
                    userId: this.data.userInfo.userId,
                    sql: 'wx_group'
                }).then(groups => {
                    if(groups.length) groups.sort((a,b) => b.sort - a.sort)
                    let allMenus = []
                    let items = new G.Item(),
                        _groups = [],
                        menus = {}
                    items._do()
                    for (let i = 0; i < groups.length; i++) {
                        items._push(next => {
                            let group = groups[i]
                            G.get('/api/model/group_menu', {
                              groupId: group.groupId,                               
                              sql: 'app_group_menu_wecom'
                            }).then(res => {
                                if (res.length) {
                                    group.menus = []
                                    for (let j = 0; j < res.length; j++) {
                                        if (res[j].url) {
                                            if (!menus[res[j].id]) {
                                                res[j].img = G.Storage.get('APIURL') + res[j].img
                                                group.menus.push(res[j])
                                                allMenus.push(res[j])
                                                menus[res[j].id] = true
                                            }
                                        }
                                    }
                                    if (group.menus.length) _groups.push(group)
                                }
                                next()
                            })
                        })
                    }
                    items._push(next => {
                        allMenus.sort((a,b) => b.sort - a.sort)
                      let i = (allMenus.length > 8) ? 7 :allMenus.length
                        this.setData({
                            groups: allMenus,
                            code:i
                        })
                        let FourList = []
                        for (let i = 0; i < allMenus.length; i++) {
                            if (allMenus[i].id == '1599287462763565056' || allMenus[i].id == '1599292417239027712' || allMenus[i].id == '1597262949767057408' || allMenus[i].id == '1597263034395529216') {
                                FourList.push(allMenus[i].id)
                            }
                        }
                        wx.setStorageSync('FourList', FourList)
                        next()
                    })
                })
            },
            load() {
                console.log('index/index');
                if (!this.data.loaded) return
                this.setData({
                    userInfo: G.Storage.get('userInfo')
                })
                if(!this.data.userInfo.userId) {
                    // G.$go('/pages/auth/auth')
                }else{
                    this.getGroups()
                    // G.get('/api/model/user', {
                    //     id: this.data.userInfo.userId,
                    //     sql: 'user_unit'
                    // }).then(user => {
                    //     this.setData({
                    //         user
                    //     })
                    // })
                    this.getcount()
                    G.get('/model/f_flow').then(res => {
                        let types = res.type.attr.actions
                        types.push({name:'其他',value:'',image:'other'})
                        this.setData({
                            types
                        })
                    })
                }
            },
            getcount(){
                G.get('/v1/task/count').then(res => {
                    let num = []
                    for(let i in res){
                        num.push(res[i].count)
                    }
                    let sum = 0
                    for (let i = 0; i < num.length; i++) {
                        sum = sum + Number(num[i])
                    }
                    this.setData({
                        count:sum
                    })
                })
            },
            onShow() {
                this.load()
            }
        },
        mounted: function (options) {
            this.data.loaded = true
            // this.load()
            // this.getSafeKnow()
            // this.getAqgg()
        }
    })
})(wx.$)